package com.springcloud.system.user.controller;

import com.springcloud.common.core.entity.R;
import com.springcloud.common.core.exception.ApiException;
import com.springcloud.common.entity.SystemRole;
import com.springcloud.common.entity.SystemUser;
import com.springcloud.system.user.service.SystemUserService;
import com.springcloud.system.user.util.SystemUserContextHolder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Set;

/**
 * <p>  </p>
 *
 * @author mori
 * @since 10/31/2019 10:24 AM
 */
@RestController
public class SystemUserController {

    private SystemUserService systemUserService;

    public SystemUserController(SystemUserService systemUserService) {
        this.systemUserService = systemUserService;
    }

    @GetMapping(value = "/user/{userId}/role")
    public R<Set<SystemRole>> getRoleListByUserId(@PathVariable("userId") Integer userId) {
        return R.ok(systemUserService.getRoleListByUserId(userId));
    }

    @GetMapping("/user/name")
    public R<SystemUser> getSystemUserByUsername(@RequestParam("username") String username) {
        return R.ok(systemUserService.getSystemUserByUsername(username));
    }

    @GetMapping("/me")
    public R<SystemUser> me() {
        SystemUser user = SystemUserContextHolder.getSystemUser();
        return R.ok(user);
    }

    @GetMapping("/ex")
    public ResponseEntity<String> ex() {
        throw new ApiException("exception info...");
    }

}
